package flc.ast.fragment;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.sign.tool.SignDataProvider;
import com.stark.sign.tool.api.SignApi;
import com.stark.sign.tool.bean.ColorBean;
import com.stark.sign.tool.bean.StyleBean;

import java.io.File;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.DraftsActivity;
import flc.ast.adapter.ColorAdapter;
import flc.ast.adapter.StyleAdapter;
import flc.ast.databinding.FragmentTextBinding;
import flc.ast.dialog.SaveDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.retrofit.IReqRetCallback;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TextFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TextFragment extends BaseNoModelFragment<FragmentTextBinding> {

    private ColorAdapter colorAdapter;
    private StyleAdapter styleAdapter;
    private int colorPos, stylePos;

    @Override
    protected int onCreate() {
        return R.layout.fragment_text;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.flDrafts.setOnClickListener(this);
        mDataBinding.tvGenerate.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.rvColorList.setLayoutManager(new GridLayoutManager(mContext, 9));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);

        mDataBinding.rvStyleList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        styleAdapter = new StyleAdapter();
        mDataBinding.rvStyleList.setAdapter(styleAdapter);
        styleAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.flDrafts://草稿箱
                startActivity(DraftsActivity.class);
                break;
            case R.id.tvGenerate://立即生成
                showSaveDialog(false);
                break;
            case R.id.tvSave://保存至草稿
                showSaveDialog(true);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == colorAdapter) {
            colorAdapter.getItem(colorPos).setSelected(false);
            colorAdapter.notifyItemChanged(colorPos);
            colorPos = position;
            colorAdapter.getItem(position).setSelected(true);
            colorAdapter.notifyItemChanged(position);
            mDataBinding.etText.setTextColor(Color.parseColor(colorAdapter.getItem(position).getValue()));
        } else if (adapter == styleAdapter) {
            styleAdapter.getItem(stylePos).setSelected(false);
            styleAdapter.notifyItemChanged(stylePos);
            stylePos = position;
            styleAdapter.getItem(position).setSelected(true);
            styleAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        List<ColorBean> listColor = SignDataProvider.getColors(getContext()).subList(0, 18);
        listColor.get(colorPos).setSelected(true);
        colorAdapter.setNewInstance(listColor);
        mDataBinding.etText.setTextColor(Color.parseColor(colorAdapter.getItem(0).getValue()));

        List<StyleBean> listStyle = SignDataProvider.getStyles(getContext());
        listStyle.get(stylePos).setSelected(true);
        styleAdapter.setNewInstance(listStyle);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myDrafts;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            mDataBinding.tvDraftsNum.setText(files.size() + "");
        } else {
            mDataBinding.tvDraftsNum.setText("0");
        }
    }

    private void showSaveDialog(boolean isDraft) {
        String content = mDataBinding.etText.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(getString(R.string.please_input_change_text));
            return;
        }
        showDialog(getString(R.string.generate_ing));
        SaveDialog dialog = new SaveDialog(mContext);
        dialog.isDraft = isDraft;

        dialog.setListener(new SaveDialog.Ilistener() {
            @Override
            public void clickSave() {
                getData();
            }
        });

        SignApi.getSignNameImgUrl(content, styleAdapter.getItem(stylePos), colorAdapter.getItem(colorPos), new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                dismissDialog();
                if (success) {
                    dialog.saveBitmap = result;
                    dialog.show();
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }

}